﻿Imports _7Wonders.Cartes

Public MustInherit Class Deck

    Private _cartes As New List(Of Carte)

    Public Property Cartes As List(Of Carte)
        Get
            Return _cartes
        End Get

        Protected Set(value As List(Of Carte))
            _cartes = value
        End Set
    End Property

    Public MustOverride Sub CreerDeck(age As Integer, nbrJoueur As Integer)

    Public Sub Brasser()
        _cartes = RandomizeList(_cartes)
    End Sub

    Public Sub AfficherCartes()
        For Each carte As Carte In Cartes
            Console.WriteLine(carte.ToString)
        Next
    End Sub

    Protected Function AjoutTypeCarte(Of T)(age As Integer, nbrJoueur As Integer) As List(Of Carte)
        AjoutTypeCarte = New List(Of Carte)

        Dim typCartes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(Function(s) s.GetTypes()).Where(Function(p) GetType(T).IsAssignableFrom(p))

        For Each typ As Type In typCartes
            If Not typ.IsAbstract Then
                Dim carte As Carte = Activator.CreateInstance(typ)
                Dim freqCarte As Integer = carte.FrequenceCarteNombreJoueur.Where(Function(w) w <= nbrJoueur).Count
                If carte.Age = age Then
                    For i As Integer = 0 To freqCarte - 1
                        AjoutTypeCarte.Add(carte)
                    Next
                End If
            End If
        Next

        Return AjoutTypeCarte
    End Function

    Private Function RandomizeList(myList As List(Of Carte)) As List(Of Carte)
        Dim r As New Random(Now.Millisecond)
        Return (From myX As Carte In myList Order By r.Next() Select myX).ToList()
    End Function

End Class
